/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) 
    {
        if ((head == NULL) || (head -> next == NULL))
            return head;

        ListNode* dummy = new ListNode(-1);
        dummy -> next = head;

        ListNode* p = head;
        ListNode* pNext = head -> next;

        while(pNext)
        {
            p -> next = pNext -> next;
            pNext -> next = dummy -> next;
            dummy -> next = pNext;
            pNext = p -> next ;
        }

        return dummy -> next;
    }
};